using Crux.Core.Dapper.SqlGenerator;

namespace Crux.Core.Dapper;
public partial class QueryRepository<TContext> where TContext : DapperContext
{
    /// <inheritdoc/>
    public int Count<TEntity>(
        Expression<Func<TEntity, bool>> predicate,
        bool ignoreFilter = false,
        Action<SqlGenerator<TEntity>>? configure = null) where TEntity : class
    {
        var generator = GetSqlGenerator(configure);

        var sqlQuery = generator.GetCount(predicate, ignoreFilter);

        return QueryFirstOrDefault<int>(sqlQuery.GetSql(), sqlQuery.Param);
    }
    /// <inheritdoc/>
    public async Task<int> CountAsync<TEntity>(
        Expression<Func<TEntity, bool>> predicate,
        bool ignoreFilter = false,
        Action<SqlGenerator<TEntity>>? configure = null,
        CancellationToken cancellationToken = default) where TEntity : class
    {
        var generator = GetSqlGenerator(configure);

        var sqlQuery = generator.GetCount(predicate, ignoreFilter);

        return await QueryFirstOrDefaultAsync<int>(sqlQuery.GetSql(), sqlQuery.Param, cancellationToken).ConfigureAwait(false);
    }
}